package com.demo.gateway.routes.predicate;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * @author: demo
 * @date: 2020年6月19日
 * @Document : 网关支持断言类
 */
@Slf4j
@Component
public class PredicateFactory {

	private Map<String, String> predicateArgs = null;

	/**
	 * 根据断言模型, 获取断言
	 * 
	 * @param predicateEnum
	 * @param args
	 * @return
	 */
	public PredicateDefinition getPredicateDefinition(PredicateEnums predicateEnum) {
		String predicateType = predicateEnum.name();
		log.info("断言类型==>{}", predicateType);
		PredicateDefinition predicateDefinition = new PredicateDefinition();
		predicateDefinition.setName(predicateType);
		if (this.predicateArgs == null) {
			this.args();
		}
		predicateDefinition.setArgs(this.predicateArgs);
		return predicateDefinition;
	}

	public void setPredicateArgs(Map<String, String> args) {
		this.predicateArgs = args;
	}

	public List<PredicateDefinition> getPredicates(PredicateEnums predicateEnum) {
		return Arrays.asList(getPredicateDefinition(predicateEnum));
	}

	public PredicateFactory args() {
		this.predicateArgs = new HashMap<String, String>(10);
		return this;
	}

	public PredicateFactory put(String key, String value) {
		this.predicateArgs.put(key, value);
		return this;
	};
}
